package com.bhms.module.datasource.hr.service;

import java.util.List;

import com.bhms.module.datasource.hr.mapper.Dept;
import com.bhms.module.datasource.hr.mapper.DeptMapper;
import com.bhms.module.datasource.hr.mapper.Person;
import com.bhms.module.datasource.hr.mapper.PersonMapper;

public class DataSourceServiceBean implements DataSourceService {

	private DeptMapper deptMapper;

	private PersonMapper personMapper;

	@Override
	public void incrementalProcess(List<Dept> deptList, List<Person> personList) throws Exception {
		deptProcess(deptList);
		personProcess(personList);
	}

	/**
	 * 组织机构增量数据处理
	 * 
	 * @param deptList
	 * @throws Exception
	 */
	private void deptProcess(List<Dept> deptList) throws Exception {
		int size = deptList != null ? deptList.size() : 0;
		for (int i = 0; i < size; i++) {
			Dept dept = deptList.get(i);
			// 根据组织机构代码判断组织机构是否存在,如果不存在保存否则更新
			Dept o = deptMapper.selectByCode(dept.getDeptid());
			if (o != null) {
				// 更新组织机构
				deptMapper.update(dept);
			} else {
				// 保存组织机构
				deptMapper.insert(dept);
			}
		}
	}

	/**
	 * 员工即经纪人增量数据处理
	 * 
	 * @param personList
	 * @throws Exception
	 */
	private void personProcess(List<Person> personList) throws Exception {
		int size = personList != null ? personList.size() : 0;
		for (int i = 0; i < size; i++) {
			Person person = personList.get(i);
			// 根据组织机构代码判断组织机构是否存在,如果不存在保存否则更新
			if (personMapper.selectByCode(person.getEmplid()) != null) {
				// 更新组织机构
				personMapper.update(person);
			} else {
				// 保存组织机构
				personMapper.insert(person);
			}
		}
	}

	@Override
	public void setDeptMapper(DeptMapper deptMapper) {
		this.deptMapper = deptMapper;
	}

	@Override
	public void setPersonMapper(PersonMapper personMapper) {
		this.personMapper = personMapper;
	}

}
